package com.levenx.web.rest;

import com.levenx.service.CommonService;
import com.levenx.service.FileService;
import com.levenx.vo.Result;
import com.levenx.vo.CommonSearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.Map;

/**
 * 通用的入口
 */
@RestController
@RequestMapping
public class CommonController {

    private Logger log = LoggerFactory.getLogger(getClass());


    @Autowired
    private Map<String, CommonService> commonServiceMap;

    @Autowired
    private FileService fileService;


    @PostMapping("/{type}/create")
    public Result create(@RequestBody Object param,
                         @PathVariable String type){
        log.debug("url->{}/create|param:{param}",type,param);
        return this.analyseService(type).create(param);
    }


    @PostMapping("/{type}/update")
    public Result update(@RequestBody Object param,
                         @PathVariable String type){
        log.debug("url->{}/update|param:{param}",type,param);
        return this.analyseService(type).update(param);
    }

    @PostMapping("/{type}/only")
    public Result only(@RequestBody CommonSearch param,
                         @PathVariable String type){
        log.debug("url->{}/only|param:{param}",type,param);
        return this.analyseService(type).only(param);
    }

    @PostMapping("/{type}/search")
    public Result search(@PathVariable String type,
                         @RequestBody CommonSearch param,
                         @RequestParam(value = "pn",defaultValue ="0" ) int pn,
                         @RequestParam(value = "ps",defaultValue = "20" ) int ps){
        log.debug("url->{}/search",type);
        return this.analyseService(type).search(param,pn,ps);
    }

    @PostMapping("/{type}/del")
    public Result delete(@RequestBody Collection param,
                         @PathVariable String type){
        log.debug("url->{}/del|param:{param}",type,param);
        return this.analyseService(type).delete(param);
    }

    /**
     * 解析出service实例
     * @return
     */
    private CommonService analyseService(String type){
        CommonService commonService = commonServiceMap.get(type+"Service");
        return null == commonService ? commonServiceMap.get("defaultService") : commonService;
    }


    @PostMapping("/upload")
    public Result<?> upload(MultipartFile file){
        return fileService.upload(file);
    }


    @GetMapping(value = "/multi/{fileName:.+}")
    public ResponseEntity<?> getFile(@PathVariable String fileName){
        return fileService.getFile(fileName);
    }


    @GetMapping("/picture/list")
    public Result<?> pictureList(){
        return fileService.list();
    }


    @GetMapping("/picture/del/{name}")
    public Result<?> pictureList(@PathVariable String name){
        return fileService.del(name);
    }

}
